﻿using System.Collections.Generic;
using AccountModule;
using Framework;
using OrderModule;
using RecipeModule;
using UnityEngine;
using UnityEngine.UI;

namespace View
{
	public class OrderView : ViewProxy
	{
		private GameObject _goBg;
		private Text _textMoney;
		private GameObject _prefabGrid;
		private OrderGrid[] _grids;
		
		private ClickTrigger _triggerRecipe;
		private ClickTrigger _triggerHot;
		private ClickTrigger _triggerInfo;
		
		protected override void SetupView()
		{
			LoadRes("Views/Order/OrderView");
		}

		protected override void OnInit()
		{
			_goBg = _transform.Find("Bg_2").gameObject;
			
			_textMoney = _transform.Find("Text_Money").GetComponent<Text>();
			_prefabGrid = _transform.Find("Scroll View/Viewport/Content/Grid").gameObject;
			_prefabGrid.SetActive(false);
			
			_triggerRecipe = ClickTrigger.Get(_transform.Find("Button_Recipe"));
			_triggerRecipe.AddClickListener(OnRecipeClick);
			_triggerHot = ClickTrigger.Get(_transform.Find("Button_Hot"));
			_triggerHot.AddClickListener(OnHotClick);
			_triggerInfo = ClickTrigger.Get(_transform.Find("Button_Info"));
			_triggerInfo.AddClickListener(OnInfoClick);
			
			Update();
		}
		
		protected override void OnDispose()
		{
			if (_grids != null)
			{
				for (var i = 0; i < _grids.Length; i++)
				{
					_grids[i].Dispose();
				}
				_grids = null;	
			}
		}
		
		private void OnRecipeClick(params object[] args)
		{
            new MyRecipeView().Show();
			Close();
		}
        
		private void OnHotClick(params object[] args)
		{
            new HotRecipeView().Show();
			Close();
		}
        
		private void OnInfoClick(params object[] args)
		{
         	new AccountView().Show();
			Close();
		}

		private void Update()
		{
			_textMoney.text = AccountManager.instance.GetCurrentAccount().money.ToString();
			List<Order> listOrder = OrderManager.instance.GetListOrder();
			if (listOrder.Count > 0)
			{
				_goBg.SetActive(true);
				
			}
			else
			{
				_goBg.SetActive(false);
			}
			UpdateGrids();
		}

		private void UpdateGrids()
		{
			if (_grids != null)
			{
				for (var i = 0; i < _grids.Length; i++)
				{
					_grids[i].Dispose();
				}
				_grids = null;	
			}
			List<Order> listOrder = OrderManager.instance.GetListOrder();
			if (listOrder.Count > 0)
			{
				_prefabGrid.SetActive(true);
				_grids = new OrderGrid[listOrder.Count];
				for (int i = 0; i < listOrder.Count; ++i)
				{
					Order order = listOrder[i];
					OrderGrid orderGrid = new OrderGrid(GameObject.Instantiate(_prefabGrid));
					orderGrid.GetTransform().SetParent(_prefabGrid.transform.parent, false);
					orderGrid.Update(order);
					_grids[i] = orderGrid;
				}
				_prefabGrid.SetActive(false);
			}
		}

		private class OrderGrid
		{
			private GameObject _gameObject;
			private RectTransform _transform;
			private GameObject[] _goLabels;
			private Text _textDate;
			private Text _textWeight;
			private Text _textMoney;
			private Text _textCalories;
			private RecipeDishUnit _dishUnit;

			public OrderGrid(GameObject gameObject)
			{
				_gameObject = gameObject;
				_transform = (RectTransform) gameObject.transform;
				
				_goLabels = new GameObject[3];
				for (int i = 0; i < 3; ++i)
				{
					GameObject goLabel = _transform.Find("Labels/Label_" + (i + 1)).gameObject;
					_goLabels[i] = goLabel;
				}
				
				_dishUnit = new RecipeDishUnit(_transform.Find("Dishes").gameObject);

				_textDate = _transform.Find("Text_Date").GetComponent<Text>();
				_textWeight = _transform.Find("Text_Weight").GetComponent<Text>();
				_textMoney = _transform.Find("Text_Money").GetComponent<Text>();
				_textCalories = _transform.Find("Text_Calories").GetComponent<Text>();
			}

			public void Dispose()
			{
				_dishUnit.Dispose();
				GameObject.Destroy(_gameObject);
			}

			public Transform GetTransform()
			{
				return _transform;
			}

			public void Update(Order order)
			{
				for (var i = 0; i < _goLabels.Length; i++)
				{
					_goLabels[i].SetActive(i == (int)order.recipeType);
				}
				_dishUnit.Update(order.listDish);
				_textDate.text = order.dateTime.Year + "." + order.dateTime.Month + "." + order.dateTime.Day;
				
				float totalWeight = 0f;
				float totalMoney = 0f;
				float totalCalories = 0f;
				for (var i = 0; i < order.listDish.Count; i++)
				{
					RecipeDish recipeDish = order.listDish[i];
					Dish dish = DishConfig.GetDish(recipeDish.dishId);
					totalWeight += dish.weight * recipeDish.count;
					totalMoney += dish.price * recipeDish.count;
					totalCalories += dish.calories * recipeDish.count;
				}
				_textWeight.text = totalWeight.ToString();
				_textMoney.text = totalMoney.ToString();
				_textCalories.text = totalCalories.ToString();
			}
		}
	}
}
